import Vue from 'vue'
import Vuex from 'vuex'
import axios from "axios"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    category:[],
    selectIndex:0,
    // subCategoryItems:[]
    aboutCategory:[],
    firstCategory:[],
    secondCategory:[]
  },
  mutations: {
    GET_ABOUT_CATEGORY_DATA(state,items){
      state.aboutCategory = [...items];

      state.aboutCategory.forEach(element => {
        if(element.pid === 0){
          state.firstCategory.push(element);
        }
      });

      var id = state.firstCategory[0].id;
      state.aboutCategory.forEach(element => {
        if(element.pid === id){
          state.secondCategory.push(element);
        }
      });
    },
    CHANGE_ABOUT_SELECT(state,item){
      state.secondCategory = [];
      var id = item.id;
      state.aboutCategory.forEach(element => {
        if(element.pid === id){
          state.secondCategory.push(element);
        }
      });
    },
    GET_CATEGORY_DATA(state,categoryItems){
      state.category = [...categoryItems];
      // state.subCategoryItems = state.category[state.selectIndex].subcategories
    },
    CHANGE_SELECT_STATUS(state,index){
      state.selectIndex = index;
      // state.subCategoryItems = state.category[state.selectIndex].subcategories
    }
  },
  actions: {
    GET_CATEGORY_DATA(context){
      axios.get("http://api.kudesoft.cn/tdk/category").then((response)=>{
        console.log(response.data.data.data)
        context.commit("GET_CATEGORY_DATA",response.data.data.data)
      }).catch((error)=>{
        console.log(error)
      })
    },
    CHANGE_SELECT_STATUS(context,index){
      context.commit("CHANGE_SELECT_STATUS",index)
    },

    GET_ABOUT_CATEGORY_DATA(context){
      axios.get("https://api.it120.cc/small4/shop/goods/category/all").then((response)=>{
        console.log(response.data)
        context.commit("GET_ABOUT_CATEGORY_DATA",response.data.data)
      }).catch((error)=>{
        console.log(error)
      })
    }
  },
  modules: {
  }
})
